using UnityEngine;
using System.Collections;

public class Frog : MonoBehaviour 
{
	public float timer;
	public float limit;	
	public float xForce;
	public float yForce;
	float xForce_Negative;
	float xForce_Positive;
	
	Vector3 startScale;
	
	public bool switcher = false;
	
	public GameObject player;
	
	void Start () 
	{
		timer = limit - 0.2f;
		xForce_Negative = -xForce;
		xForce_Positive = xForce;
		
		startScale = transform.localScale;
	}
	
	void Update () 
	{
		if(switcher)
		{
			timer += Time.deltaTime;
			
			if(timer > limit)
			{
				rigidbody.AddForce(-xForce,yForce,0);
				timer = 0;
			}
		}
		
		if(transform.position.x > player.transform.position.x)
		{
			xForce = xForce_Positive;
			transform.localScale = new Vector3(startScale.x, startScale.y, startScale.z);
		}
		
		if(transform.position.x < player.transform.position.x)
		{
			xForce = xForce_Negative;
			transform.localScale = new Vector3(-startScale.x, startScale.y, startScale.z);
		}
	}
	
	public void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			switcher = true;
			rigidbody.useGravity = true;
		}
	}
	
	public void OnTriggerExit(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			switcher = false;
			rigidbody.useGravity = false;
		}
	}
}
